/**
 * @Title: multipleSelect_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2015 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author ex-xieqi001
 * @date 2015年1月22日 上午10:01:57
 * @version V1.0
 * 
 * 

 */
;
(function(){
	
	'use strict';
	
	var app = angular.module('fwa.directives');
	app.directive('multipleSelect', ['BaseURL','$parse',function(baseUrl,$parse){
		
		function linker($scope, elm, attrs, ctrl) {
			
			//所有需要展示的数据集合
			$scope.containerDatas  = [];
			
			//已经选中的值
			$scope.selectDatas =  [];
			
			//对象唯一性的属性名称
			$scope.soleName = $scope.soleName || 'id';
			
			//按钮距离上边距的距离
			$scope.buttonTop = $scope.buttonTop || 50;
			$scope.paddingTop =  parseInt($scope.buttonTop);
			
			//显示的对象属性集合
			$scope.displaySet = $scope.displaySet || [{name: 'name',width: '100%'}];
			
			//监听未选中的集合
			$scope.$watchCollection("containerData",function(v) {
				$scope.containerDatas  = v || [];
			});
			
			//是否是第一次赋值
			var isSelect = true; 
			
			//获取赋值默认选中
			function getSelectDatas(v) {
				var length = $scope.containerData.length;
				var datas = [];
				for (var i = 0; i < v.length; i++) {
					if ((typeof v[i]) === "string") {
						for (var j = 0; j < length; j++) {
							var data = $scope.containerData[j];
							if (data[$scope.soleName] === v[i]) {
								datas.push(data);
							}
						}
					}
				}
				return datas;
			}
			
			//左边删除右边已经选中的值
			function getContainerDatas() {
				var datas = [];
				angular.forEach($scope.containerData,function(data,i) {
					var isFlag = true;
					angular.forEach($scope.selectDatas,function(selectData,j){
						if (data[$scope.soleName] === selectData[$scope.soleName] ) {
							isFlag = false;
						}
					});
					if (isFlag) {
						datas.push(data);
					}
				});
				$scope.containerDatas = datas;
			}
			
			//监听已选中的集合
			$scope.$watchCollection("selectData",function(v) {
				if (v) {
					if (isSelect) {
						isSelect = false;
						if ((typeof v) === "string") {
							var datas = v.split(",");
							$scope.selectDatas = getSelectDatas(datas);
							getContainerDatas();
						}
						else {
							$scope.selectDatas = getSelectDatas(v);
							getContainerDatas();
						}
					}
					else {
						//如果选中的值与反馈的值不一致时
						if ($scope.selectDatas.length != $scope.selectData.length) {
							getContainerDatas();
						}
					}
				}
				else {
					$scope.selectDatas = [];
				}
			});
			
			//搜索已选中框中是否已经存在该对象
			function findSelectData(data) {
				var length = $scope.selectDatas.length;
				for (var i = 0; i < length; i++ ) {
					var selectData = $scope.selectDatas[i];
					if (data[$scope.soleName] === selectData[$scope.soleName] ) {
						return i;
					}
				}
				return -1;
			}
			//搜索未选中框中是否已经存在该对象
			function findData(data) {
				var length = $scope.containerDatas.length;
				for (var i = 0; i < length; i++ ) {
					var containerData = $scope.containerDatas[i];
					if (data[$scope.soleName] === containerData[$scope.soleName] ) {
						return i;
					}
				}
				return -1;
			}
			
			/**
			 * 把左边所有的数据添加到右边
			 */
			$scope.addAll = function() {
				var length = $scope.containerDatas.length;
				angular.forEach($scope.containerDatas,function(data,i) {
					var index = findSelectData(data);
					if (index === -1) {
						$scope.selectDatas.push(data);
					}
					if ((i + 1) === length) {
						$scope.containerDatas = [];
					}
				});
				$scope.selectData = $scope.selectDatas;
			}
			
			/**
			 * 把右边所有的数据添加到左边
			 */
			$scope.delAll = function() {
				var length = $scope.selectDatas.length;
				angular.forEach($scope.selectDatas,function(data,i) {
					var index = findData(data);
					if (index === -1) {
						$scope.containerDatas.push(data);
					}
					if ((i + 1) === length) {
						$scope.selectDatas = [];
					}
				});
				$scope.selectData = $scope.selectDatas;
			}
			
			/**
			 *把左边选中的数据添加到右边
			 */
			$scope.add = function() {
				var datas = [];
				angular.forEach($scope.containerDatas,function(data,i) {
					if (data.checked == true) {
						var index = findSelectData(data);
						if (index === -1) {
							data.checked = false;
							$scope.selectDatas.push(data);
						}
						else {
							datas.push(data);
						}
					}
					else {
						datas.push(data);
					}
				});
				$scope.containerDatas = datas;
				
				$scope.selectData = $scope.selectDatas;
			}

			/**
			 * 把右边选中的数据添加到左边
			 */
			$scope.del = function() {
				var datas = [];
				angular.forEach($scope.selectDatas,function(data,i) {
					if (data.checked == true) {
						var index = findData(data);
						if (index === -1) {
							data.checked = false;
							$scope.containerDatas.push(data);
						}
						else {
							datas.push(data);
						}
					}
					else {
						datas.push(data);
					}
				});
				$scope.selectDatas = datas;
				$scope.selectData = $scope.selectDatas;
			}
			
			/**
			 * 点击一个对象判断是否需要选中
			 */
			$scope.checkData = function(data) {
				if (!data.checked || data.checked == false) {
					data.checked = true;
				}
				else if (data.checked == true) {
					data.checked = false;
				}
			}
			
		}
		return {
			templateUrl: baseUrl + 'views/hcpms/common/directives/multipleSelect_view.html',
			restrict: 'E',
			scope: {
				soleName: '@soleName',
				buttonTop: '@buttonTop',
				containerData: '=containerData',
				selectData: '=selectData',
				displaySet: '=displaySet'
			},
			replace: true,
			link: linker
		};
		

	}]);
	
})();